package com.itheima.web;

import com.itheima.entity.Result;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/orderSetting")
@PreAuthorize("hasRole('admin')")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @PostMapping("/import")
    public Result importData(@RequestParam("file") MultipartFile file){
        List<String[]> data = null;
        try {
            data = POIUtils.readExcel(file.getInputStream(),file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(orderSettingService.importData(data));
    }

    @GetMapping("/selectDataByMonth/{year}/{month}")
    public Result selectDataByMonth(@PathVariable("year") int year,@PathVariable("month") int month){
        return new Result(orderSettingService.selectDataByMonth(year,month));
    }

    @GetMapping("/daySetting/{day}/{num}")
    public Result daySetting(@PathVariable("day") String day, @PathVariable("num") Integer num){
        return new Result(orderSettingService.daySetting(day,num));
    }

}
